package com.ruoyi.match.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.match.job.service.RealTimeMatchTaskService;
import com.ruoyi.match.match.domain.MatchCollectionDate;
import com.ruoyi.match.utils.BrowserUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author sjz
 * @version 1.0.0
 * @desc
 * @date 2023/11/5 15:13
 **/

@Slf4j
@Component
@RequiredArgsConstructor
public class RealTimeMatchTask {

    private final RealTimeMatchTaskService realTimeMatchTaskService;

    @XxlJob("realTimeMatchTask")
    private void process() throws IOException {
        LocalDate startDate = LocalDate.now();
        // 获取参数
        String matchDate = XxlJobHelper.getJobParam();
        if(StrUtil.isNotBlank(matchDate)){
            startDate = LocalDate.parse(matchDate, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
        }

        WebDriver browser = BrowserUtil.createBrowser();
        try {
            realTimeMatchTaskService.getRealTimeMatchInfo(browser,startDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            log.warn("浏览器退出");
            browser.quit();
        }

    }
}
